/*-------------------------------------------------------------------------

   File :          CSCLNT.h
   Project :       VeriFone SoftPay Base
   Description :   TCP Client Server Functions
   Developer :     Ravindra Rao (ravindra_r1@verifone.com)


***************** Modification History ***********************************

$Log: /SoftPay/Source/Include/CSCLNT.h $

**************************************************************************

   Copyright (C) 2003 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/



#define SPBASE_TIMER1 0xFFFF7FFF
#define SPBASE_TIMER2 0xFFFF8FFF
#define COMM_SVR "COMMSVR"

// Misc Defn.
#define CSCLNT_MSEC_100	100L
#define CSCLNT_MSEC_250	250L
#define CSCLNT_MSEC_500	500L
#define CSCLNT_MSEC_1000	1000L

// Internal Error Code
// All Error codes will be in the form CSCLNT_XXXXxxxx (Max 8 chars after the understscore)
// The base for all error codes is 1000 and is arbitrary
#define CSCLNT_NOERROR		1000	// No Error
#define CSCLNT_NOEVENT		1001	// No VMAC Event
#define CSCLNT_INVALID		1002	// Invaild value
#define CSCLNT_TIMEOUT		1003	// Timeout has occurred
#define CSCLNT_BUFSIZE		1004	// Buffer is not sufficient
#define CSCLNT_BADSNDR		1005	// Unknown sender of data, expecting from TCP Server
#define CSCLNT_BADEVENT	1006	// Unexpected event from server
#define CSCLNT_SENDERR		1007	// Send event err

// Actions for Add Extra Data
#define ADD_CONNECT_DATA                1
#define ADD_DISCONNECT_DATA          2
#define ADD_SEND_DATA                      3
#define ADD_DOWNLOAD_DATA            4

#define MAX_ESM_RESP_EVENTS	1		// Made this to an array so that it is extensible in future
#define COMMSVR_EVENT_DATA_SIZE		500

// shVCS_Download Error Return Values
#define VCS_DL_ERR_NO_CSCLNT_COMM -1001
#define VCS_DL_ERR_ADD_DOWNLOAD_DATA -1002
#define VCS_DL_ERR_SEND_EVENT_TO_SVR -1003
#define VCS_DL_ERR_READ_EVENT_FROM_SVR -1004
#define VCS_DL_ERR_VCS_EVT_DL_RESP -1005

//for signal strenght
#define WIFI_CHAR1		(char)22 // Jimmy - Added for gprs logo
#define NO_WIFI_CHAR1		(char)19 // Jimmy - Added for no gprs logo
#define NO_ANTENNA_CHAR1		(char)4  // Jimmy - Added for no signal logo


typedef struct CSCLNT_struct{
	//-- Host data
	short shECTIndex; // Index of Ethernet Communication Table
	//-- Host Status
	short bConnStatus;  // current connection status
	//-- Server Context
	short bInSession; // boolean flag to indicate when in session and shSessHandle is valid
	short shSessHandle; // session handle returned by server
	//-- Event state machine
	short shEsmCount; // number of events in the event state machine list
	short shEsmList[MAX_ESM_RESP_EVENTS]; // event state machine list
	//-- VMAC Event -- contain data when successful
	unsigned int inEventID; // Event returned from the server
	unsigned char EventDataBuf[COMMSVR_EVENT_DATA_SIZE]; // Event Buffer
	//unsigned int inEventDataSize; // Size of data in event buffer
	unsigned short inEventDataSize; // Size of data in event buffer
	unsigned char EventSenderName[20]; // event sender name MAX_TASK_NAME_SIZE
	//-- Error Handling -- contain data under error conditions
	short shTCPClientErr; // Internal error code
	int inNativeErr; // OS / Library / SSL error codes
} CSCLNT_OBJECT;

// Support functions
void vdInitializeCSCLNTComm (COMM_OBJECT *obCommunication);
short shVCS_Download ( unsigned char dl_type, unsigned int dl_sec );
int inSignalStrength (TRANSACTION_OBJECT * pobTran);
int inDoCommSvrStatus(void);

// Interface Implementation
int inInitCSCLNTComm(COMM_OBJECT *obCommunication);
int inDeInitCSCLNTComm(COMM_OBJECT *obCommunication);
int inEndCSCLNTComm (COMM_OBJECT *obCommunication);
int inFlushCSCLNTComm(void);
int inBeginCSCLNTComm (COMM_OBJECT *obCommunication, int inIndex, VS_BOOL fUnusedParameter);
int inCheckCSCLNTComm (COMM_OBJECT *obCommunication);
int inSendCSCLNTComm (COMM_OBJECT *obCommunication, char *pchSendBuff, int inSendSize,
                        VS_BOOL fConfirmSend, int inTimeout, long lnMessage);
int inReceiveCSCLNTComm (COMM_OBJECT  *obCommunication, char *pchReceiveBuff,
						   int inReceiveSize, int inReceiveTimeout, long lnMessage);

// CS Client Specific local functions
static short shVerifySender(CSCLNT_OBJECT *pobCSCLNT);
static short shVerifyEvent(int inEventID, CSCLNT_OBJECT *pobCSCLNT);
static short shSendEventToSvr(unsigned int inSendEvent, CSCLNT_OBJECT *pobCSCLNT, VS_BOOL fRetry);
static short shReadEventFromSvr(long msRespTimeout, CSCLNT_OBJECT *pobCSCLNT);
static void vdEthrDisplayMessage(long lnMsgIndex, int inErr);
static void vdReadPendingEventsInQ(void);
static short shSendDataToClient(const char *pchSendBuff, const int inSendSize, CSCLNT_OBJECT *pobCSCLNT);
static short shRecvDataFromClient(char *pchReceiveBuff, int inRecvSize, CSCLNT_OBJECT *pobCSCLNT);
static short shGetVersion(CSCLNT_OBJECT *pobCSCLNT, unsigned char *pszVersion);
static short shGetStatus(CSCLNT_OBJECT *pobCSCLNT);
static short shRequestStatus(CSCLNT_OBJECT *pobCSCLNT, VS_BOOL fRetry);
short shExtractStatus(CSCLNT_OBJECT *pobCSCLNT);

int inCheckStatus(TRANSACTION_OBJECT *pobTran);
int inDisplaySignalStrength(TRANSACTION_OBJECT *pobTran);
int inInitCommServer(TRANSACTION_OBJECT *pobTran);